#include "BatManage.h"

// uint16 AdcSum;
// uint8 AdcMax;
// uint8 AdcMin;
// uint8 Adc
Typedef_ADC ADC;

void    initBat(void)
{
    ADC.Sum = 0;
    ADC.Max = 0;
    ADC.Min = 255;
    ADC.Sample = 0;
    ADC.Flg_Update = NO;

    BatV = 30;
    f_Low_Power = NO;
}

void    CheckBatHealth(void)
{
	u8 adc_result;

    //---- 读取 ADC 值 ----//
	ADC_GO();
	while (ADC_IS_BUSY);
	adc_result = ADC_GetADCResult()>>4;
    if(adc_result >= ADC.Max)   ADC.Max = adc_result;
    if(adc_result < ADC.Min)    ADC.Min = adc_result;
    ADC.Sum += adc_result;
    ADC.Sample++;
    if(ADC.Sample >= 10)
    {
        ADC.Sample = 0;
        ADC.Sum -= ADC.Max;
        ADC.Sum -= ADC.Min;
        ADC.Result = ADC.Sum >> 3;
        ADC.Flg_Update = YES;
        ADC.Sum = 0;
        ADC.Max = 0;
        ADC.Min = 255;
    }

    if(ADC.Flg_Update)
    {
        //---- 计算电池电压值 ----//
	    u16 temp;
        ADC.Flg_Update = NO;
        temp = (12 * 256) / ADC.Result;
        BatV = temp;
				BatV = C_LOW_POWER_V+10;
        //---- 判断低压 ----//
        if(BatV < C_LOW_POWER_V)
        {
            f_Low_Power = YES;
        }
        else if(BatV >= C_LOW_POWER_RESUME_V)
        {
            f_Low_Power = NO;
        }
    }
}